<template>
  <div class="flex-1 flex flex-col h-full">
    <div class="flex-1 overflow-y-auto">
      <div class="p-3">
        <!-- 分类管理 -->
        <div class="mb-6">
          <div class="flex justify-between items-center mb-3">
            <h3 class="text-sm font-medium text-gray-900">地址分类管理</h3>
            <a-button @click="addNewCategory" size="mini" type="primary">
              添加分类
            </a-button>
          </div>

          <div class="space-y-2">
            <div v-for="(category, index) in store.addressCategories.sort((a: any, b: any) => a.order - b.order)"
              :key="category.id" class="border border-gray-200 rounded-md p-3">
              <div class="flex items-center space-x-2 mb-2">
                <!-- 分类名称 -->
                <a-input :model-value="category.name"
                  @update:model-value="(value) => updateCategory(category.id, { name: value })" size="small"
                  class="flex-1" />
              </div>

              <div class="flex justify-between items-center">
                <span class="text-xs text-gray-500">
                  使用该分类的地址: {{store.addressList.filter((a: any) => a.categoryId === category.id).length}} 个
                </span>
                <div class="space-x-1">
                  <a-button @click="moveCategory(category.id, 'up')" size="mini" type="outline" :disabled="index === 0">
                    ↑
                  </a-button>
                  <a-button @click="moveCategory(category.id, 'down')" size="mini" type="outline"
                    :disabled="index === store.addressCategories.length - 1">
                    ↓
                  </a-button>
                  <a-button @click="deleteCategory(category.id)" size="mini" status="danger" type="outline"
                    :disabled="category.id === 'default'">
                    删除
                  </a-button>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 标签管理 -->
        <div class="flex justify-between items-center mb-3">
          <h3 class="text-sm font-medium text-gray-900">地址标签管理</h3>
          <a-button @click="addNewTag" size="mini" type="primary">
            添加标签
          </a-button>
        </div>

        <div class="space-y-2">
          <div v-for="(tag, index) in store.addressTags.sort((a: any, b: any) => a.order - b.order)" :key="tag.id"
            class="border border-gray-200 rounded-md p-3">
            <div class="flex items-center space-x-2 mb-2">
              <!-- 标签颜色 -->
              <input type="color" :value="tag.color"
                @input="(e) => updateTag(tag.id, { color: (e.target as HTMLInputElement).value })"
                class="w-6 h-6 rounded border" />
              <!-- 标签名称 -->
              <a-input :model-value="tag.name" @update:model-value="(value) => updateTag(tag.id, { name: value })"
                size="small" class="flex-1" />
              <!-- 标签符号 -->
              <a-input :model-value="tag.symbol"
                @update:model-value="(value) => updateTag(tag.id, { symbol: value.slice(0, 1) })" size="small"
                class="w-12 text-center" placeholder="符" />
            </div>

            <div class="flex justify-between items-center">
              <span class="text-xs text-gray-500">
                使用该标签的地址: {{store.addressList.filter((a: any) => a.status === tag.id).length}} 个
              </span>
              <div class="space-x-1">
                <a-button @click="moveTag(tag.id, 'up')" size="mini" type="outline" :disabled="index === 0">
                  ↑
                </a-button>
                <a-button @click="moveTag(tag.id, 'down')" size="mini" type="outline"
                  :disabled="index === store.addressTags.length - 1">
                  ↓
                </a-button>
                <a-button @click="deleteTag(tag.id)" size="mini" status="danger" type="outline"
                  :disabled="tag.id === 'unchecked' || tag.id === 'checked'">
                  删除
                </a-button>
              </div>
            </div>
          </div>
        </div>

        <div class="mt-4 p-2 bg-gray-50 rounded text-xs text-gray-600">
          <p class="mb-1">提示：</p>
          <p>• 默认标签（未打卡、已打卡）不能删除</p>
          <p>• 新添加的地址默认使用第一个标签</p>
          <p>• 标签符号建议使用单个字符</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { watch } from 'vue'
import { useMapStore } from '../store'
import { addNewCategory, deleteCategory, moveCategory, updateCategory } from '../store/categories'
import { renderAddressMarkers } from '../store/map'
import { addNewTag, deleteTag, moveTag, updateTag } from '../store/tags'

const store = useMapStore()

// 监听标签变化，重新渲染地图标记
watch(() => store.addressTags, () => {
  if (store.map) {
    renderAddressMarkers()
  }
}, { deep: true })
</script>
